from urllib import parse
import tkinter.messagebox as msgbox
import tkinter as tk
import webbrowser
import re


class APP:
    def __init__(self, width=500, height=300):
        self.w = width
        self.h = height
        self.title = '追剧'
        self.root = tk.Tk(className=self.title)

        self.url = tk.StringVar()

        self.v = tk.IntVar()

        self.v.set(1)

        frame_1 = tk.Frame(self.root)
        frame_2 = tk.Frame(self.root)
        frame_3 = tk.Frame(self.root)

        menu = tk.Menu(self.root)
        self.root.config(menu=menu)
        moviemenu = tk.Menu(menu, tearoff=0)
        menu.add_cascade(label='友情链接', menu=moviemenu)

        moviemenu.add_command(label='腾讯视频', command=lambda: webbrowser.open('http://v.qq.com/'))
        moviemenu.add_command(label='B站', command=lambda: webbrowser.open('http://www.bilibili.com/'))

        group = tk.Label(frame_1, text='请选择一个视频破解方式', padx=10, pady=10)
        tb1 = tk.Radiobutton(frame_1, text='通道1', variable=self.v, value=1, width=10, height=3)
        tb2 = tk.Radiobutton(frame_1, text='通道2', variable=self.v, value=2, width=10, height=3)
        tb3 = tk.Radiobutton(frame_1, text='通道3', variable=self.v, value=3, width=10, height=3)
        label1 = tk.Label(frame_2, text='输入视频链接：')
        entry = tk.Entry(frame_2, textvariable=self.url, highlightcolor='Fuchsia', highlightthickness=1, width=35)
        label2 = tk.Label(frame_2, text=' ')
        play = tk.Button(frame_2, text='播放', font=('楷体', 12), fg='Purple', width=2, height=1, command=self.video_play)
        label3 = tk.Label(frame_2, text=' ')
        label_explain = tk.Label(frame_3, fg='red', font=('楷体', 18), text='学习交流')
        label_warning = tk.Label(frame_3, fg='black', font=('楷体', 16), text='\n\n')

        frame_1.pack()
        frame_2.pack()
        frame_3.pack()
        group.grid(row=0, column=0)
        tb1.grid(row=0, column=1)
        tb2.grid(row=0, column=2)
        tb3.grid(row=0, column=3)
        label1.grid(row=0, column=0)
        entry.grid(row=0, column=1)
        label2.grid(row=0, column=2)
        play.grid(row=0, column=3, ipadx=10, ipady=10)
        label3.grid(row=0, column=4)
        label_explain.grid(row=1, column=0)
        label_warning.grid(row=2, column=0)

    def video_play(self):
        port_1 = 'http://jx.quanmingjiexi.com/?url='
        port_2 = 'http://www.wmxz.wang/video.php?url='
        port_3 = 'http://www.ckmov.vip/api.php?url='

        if re.match(r'^https?:/{2}\w.+$', self.url.get()):
            if self.v.get() == 1:
                ip = self.url.get()
                ip = parse.quote_plus(ip)
                webbrowser.open(port_1 + self.url.get())
            elif self.v.get() == 2:
                ip = self.url.get()
                ip = parse.quote_plus(ip)
                get_url = 'http://www.wmxz.wang/video.php?url=%s' % ip
                webbrowser.open(get_url)
            elif self.v.get() == 3:
                ip = self.url.get()
                ip = parse.quote_plus(ip)
                get_url = 'http://www.ckmov.vip/api.php?url=%s' % ip
                webbrowser.open(get_url)
        else:
            msgbox.showerror(title='错误', message='视频地址无效，请重新输入！')

    def center(self):
        ws = self.root.winfo_screenwidth()
        hs = self.root.winfo_screenheight()
        x = int((ws / 2) - (self.w / 2))
        y = int((hs / 2) - (self.h / 2))
        self.root.geometry('{}x{}+{}+{}'.format(self.w, self.h, x, y))

    def loop(self):
        self.root.resizable(False, False)
        self.center()
        self.root.mainloop()


if __name__ == '__main__':
    app = APP()
    app.loop()
